package com.ynr.invoice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.ynr.common.utils.BaseController;
import com.ynr.dao.IInvoiceDao;
import com.ynr.invoice.service.IInvoiceService;
import com.ynr.voucher.dao.IDepreciationDao;

@Controller
public class InvoiceController extends BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(InvoiceController.class);

	@Autowired
	private IInvoiceService invoiceService;
	
	@Autowired 
	private IInvoiceDao invoiceDao;
	
	@Autowired
	private IDepreciationDao depreciationDao;
	
	@RequiresRoles({"invoiceInput"})
	@RequestMapping(value="/getPendingInvoice", method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> getPendingInvoice() {
        return invoiceDao.getPendingInvoice();
	}
	
	@RequestMapping("/uploadInvoice")
	@ResponseBody
	public Map<String, Object> uploadInvoice(DefaultMultipartHttpServletRequest multipartRequest,
			HttpServletRequest request) {
		logger.debug("entrance uploadInvoice");
		List<MultipartFile> multipartFileList = multipartRequest.getFiles("files");
		String period = request.getParameter("period");
		int companyId = Integer.valueOf(request.getParameter("company_id"));
		String rootPath = "/home/files/invoice";
		if (!multipartFileList.isEmpty()) {
			for(MultipartFile multifile : multipartFileList) {
				invoiceService.uploadInvoice(period, companyId, rootPath, multifile);
			}
		}
		Map<String, Object> retMap = new HashMap<>();
		retMap.put("url", "");
		return retMap;
	}
	
	@RequestMapping("/updateInvoiceBasicInfo")
	@ResponseBody
	public Map<String, Object> updateInvoiceBasicInfo(HttpServletRequest request) {
		Map<String, String[]> dataMap = request.getParameterMap();
		invoiceService.updateInvoiceBasicInfo(dataMap);
		return null;
	}
	
	@RequestMapping("/getUnrecognizedInvoices")
	@ResponseBody
	public Map<String, Object> getUnrecognizedInvoices() {
		return invoiceService.getUnrecognizedInvoices();
	}
	
	@RequestMapping("/deleteInvoice")
	@ResponseBody
	public Map<String, Object> deleteInvoice(HttpServletRequest request) {
		logger.debug(request.getParameter("idList"));
		List<String> idList = (List)JSONArray.parse(request.getParameter("idList"));
		return invoiceService.deleteInvoice(idList);
	}
	
	@RequestMapping("/manualInput")
	@ResponseBody
	public Map<String, Object> manualInput(HttpServletRequest request) {
		String fphm = request.getParameter("fphm");
		String fpdm = request.getParameter("fpdm");
		String kprq = request.getParameter("kprq");
		String checkCode = request.getParameter("checkCode");
		String noTaxAmount = request.getParameter("noTaxAmount");
		String billDate = request.getParameter("bill_date");
		String companyId = request.getParameter("company_id");
		logger.debug("fphm : {}, fpdm : {}, kprq : {}, checkCode : {}, noTaxAmount : {}, billDate : {}, companyId : {}", fphm,fpdm, kprq, checkCode, noTaxAmount, billDate, companyId);
		return invoiceService.manualInput(fphm, fpdm, kprq, checkCode, noTaxAmount, billDate, companyId);
	}
	
	@RequestMapping("/getInvoiceVoucher")
	@ResponseBody
	public Map<String, Object> getInvoiceVoucher(HttpServletRequest request) {
		return null;
	}
	
	@RequestMapping("/getFaDepreciation")
	@ResponseBody
	public Map<String, Object> getFaDepreciation(HttpServletRequest request) {
		String companyId = request.getParameter("company_id");
		String billDate = request.getParameter("bill_date");
		List<Map<String, Object>> faList = depreciationDao.getFaDepreciation(companyId, billDate);
		Map<String, Object> retMap = new HashMap<>();
		retMap.put("data", faList);
		return retMap;
	}
}